//
//  RemoteShutDownViewController.m
//  Leo01
//
//  Created by 张旭 on 15/9/30.
//  Copyright (c) 2015年 张旭. All rights reserved.
//

#import "RemoteShutDownViewController.h"
#import "LeoMsg.h"


#import "AppDelegate.h"
#import "SyncManager.h"
#import "DataManager.h"

@interface RemoteShutDownViewController ()

@property (nonatomic,retain) SyncManager *syncManager;
@property (nonatomic,retain) DataManager *dataManager;

@end

@implementation RemoteShutDownViewController
@synthesize popUpView;
@synthesize motherWindow;
@synthesize cancelButton;
@synthesize confirmButton;

-(void)dealloc
{
    self.syncManager=NULL;
    self.dataManager=NULL;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setBackGroudView];
    [self initDataProxy];
    [self initButtonActions];
    // Do any additional setup after loading the view from its nib.
}

- (void)setBackGroudView{
    [self.view setFrame:[UIScreen mainScreen].bounds];
    [self.view setBackgroundColor:[UIColor colorWithWhite:0.0 alpha:0.4]];
    self.popUpView.layer.cornerRadius = 8;
}

-(void)initButtonActions{
    [confirmButton addTarget:self action:@selector(submitAction) forControlEvents:UIControlEventTouchDown];
    [cancelButton addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchDown];
}
-(void)initDataProxy{
    self.syncManager=[[SyncManager alloc] init];
    self.dataManager=[[DataManager alloc] init];
}
-(void)submitAction
{

    
    AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
    myDelegate.silentDetailsDataArray = [[NSMutableArray alloc]init];
        
    User *user=[User sharedSingleton];
    NSString *userId=user.uid;
    Watch *watch=[self.dataManager getActiveWatch:userId];
    NSString *watchId=watch.deviceUid;
    
    self.motherWindow.hidden=YES;
    //CommandType
    
    [self.syncManager sendCommand:watchId type:REMOTE_SHUTDOWN  watchParams:NULL completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
        if (result==SUCESS)
        {
            NSLog(@"here");
            [LeoMsg showMsg:@"远程关机成功!"];
        }
        else
        {
            if(result==NODATA)
            {
                [LeoMsg showMsg:@"当前网络异常，请检查网络"];
            }
            else
            {
                if(description!=NULL)
                {
                    if([description isEqualToString:DEVICE_OFFLINE_INFO])
                        [LeoMsg showMsg:@"手表已处于关机状态"];
                    else
                        [LeoMsg showMsg:description];
                }
                else
                    [LeoMsg showMsg:@"远程关机失败"];
            }

        }
        NSLog(@"here");

    }];
}

-(void)cancelAction
{
    self.motherWindow.hidden=YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    if(self.view.window==nil)
    {
        self.view=nil;
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
